import { defineStore } from 'pinia'
export const useWebStore = defineStore({
    id: 'websites',
    state: () => ({
        theme: 'light',
        websites: [],
        downloadList: [],
        sidebarStatus: true,

    }),
    actions: {
        setTheme(bool) {
            this.theme = bool ? 'dark' : 'light'
        },
        setWebSite(obj) {
            this.websites.unshift(obj)
        },
        changeSidebar() {
            this.sidebarStatus = !this.sidebarStatus
        },
        updateDownloadList(path) {
            this.downloadList.push(path)
        },
        updateFavorite(url) {
            const favoriteItem = this.websites.find(item => item.url === url)
            favoriteItem.favorite = !favoriteItem.favorite
        },
        removeBookmark(url) {
            const deleteItem = this.websites.find(item => item.url === url)
            deleteItem.isDelete = true
        },
        addBookmark(url) {
            const deleteItem = this.websites.find(item => item.url === url)
            deleteItem.isDelete = false
        },
        clearRubbish(url) {
            return new Promise(resovle => {
                if (url) {
                    const deleteIndex = this.websites.findIndex(item => item.url === url)
                    this.websites.splice(deleteIndex, 1)
                    resovle('删除书签成功')
                } else {
                    const dataList = this.websites.filter(item => !item.isDelete)
                    this.websites = dataList
                    resovle('回收站清空成功')
                }

            })
        }
    }
})